<template>
  <el-dialog
      width="500px"
      :visible="visible"
      :lock-scroll="false"
      v-dialog-movable="false"
      custom-class="ele-dialog-form"
      :title="(isUpdate?'编辑' : '添加') + '管理员'"
      @update:visible="updateVisible"
      :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-width="60px">
      <el-form-item label="账号:" prop="UserName">
        <el-input clearable v-model="form.UserName" placeholder="请输入账号"/>
      </el-form-item>
      <el-form-item label="昵称:" prop="NickName">
        <el-input clearable v-model="form.NickName" placeholder="请输入昵称"/>
      </el-form-item>
      <el-form-item label="密码:" prop="UserPassword" v-if="!isUpdate">
        <el-input type="password" clearable autocomplete="new-password" v-model="form.UserPassword"
                  placeholder="请输入密码"/>
      </el-form-item>
      <el-form-item label="角色:" prop="RoleID">
        <el-select v-model="form.RoleID" class="ele-fluid" multiple placeholder="请选择角色" :loading="roleLoading">
          <el-option v-for="item in role" :key="item.RoleID" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态:" prop="UserStatus">
        <el-radio-group v-model="form.UserStatus">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="2">删除</el-radio>
          <el-radio :label="3">禁用</el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import rules from '@/config/rules/users/save-user'

export default {
  name: 'sys-user-save',
  props: {
    // 弹窗是否打开
    visible: {
      type: Boolean,
      default: false
    },
    // 修改回显的数据
    data: {
      type: Object,
      default: () => {
        return {}
      }
    },
  },
  data () {
    return {
      // 表单数据
      form: this.initFormData(this.data),
      // 表单验证规则
      rules,
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      role: [],
      roleLoading: false
    }
  },
  watch: {
    data () {
      this.isUpdate = !!(this.data && this.data.UserID)
      this.form = this.initFormData(this.data)
    },
    visible () {
      if (this.visible) {
        this.loadRoles()
      }
    }
  },
  methods: {
    loadRoles () {
      if (this.role.length) return false
      this.roleLoading = true
      this.$http.get('/role/select').then(res => {
        this.roleLoading = false
        if (res.data.code === 200) {
          this.role = res.data.result.select
        }
      })
    },
    /* 保存编辑 */
    save () {
      this.$refs['form'].validate((valid) => {
        if (!valid) return false
        let data = {
          RoleID: this.form.RoleID,
          UserName: this.form.UserName,
          NickName: this.form.NickName,
          UserStatus: this.form.UserStatus,
        }
        if (this.isUpdate) {
          data.UserID = this.form.UserID
        } else {
          data.UserPassword = this.form.UserPassword
        }
        this.loading = true
        let requestUrl = this.isUpdate ? '/users/update' : '/users/create'
        this.$http.post(requestUrl, data).then(res => {
          this.loading = false
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
            this.updateVisible(false)
            this.$emit('done')
            this.form = this.initFormData({})
          }
        }).catch(() => {
          this.loading = false
        })
      })
    },
    /* 更新visible */
    updateVisible (value) {
      this.$emit('update:visible', value)
    },
    /* 初始化form数据 */
    initFormData (data) {
      let form = {
        UserStatus: 1
      }
      if (data) {
        Object.assign(form, data)
        form.UserStatus = Number(form.UserStatus)
        if (this.isUpdate && !form.UserPassword) {
          form.UserPassword = '1234567'
        }
      }
      return form
    },
  }
}
</script>
